﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using IHISCETeim.BLL;
using IHISCETeim.Library;
using IHISCETeim.Model;
using System.Data;


namespace IHISCETeim.Web.MachineSys.UserModel
{
    public partial class UClaimDemageControlSupplerTongJi : PageBase
    {
        protected override Permission[] PowerKey
        {
            get
            {
                return new Permission[] { Permission.查看 };
            }
        }
        protected override string DWZRef
        {
            get
            {
                return "UClaimDemageControlList";
            }
        }
        #region 属性 (查询条件)

        protected override string orderField
        {
            get
            {
                return string.IsNullOrEmpty(base.orderField) ? "FaultDate" : base.orderField;
            }
        }
        protected override string orderDirection
        {
            get
            {
                return string.IsNullOrEmpty(base.orderDirection) ? "Desc" : base.orderDirection;
            }
        }

        protected string DownlineDateFrom
        {
            get
            {
                return GetRequestValue<string>("DownlineDateFrom", GetQuarterDate(DateTime.Now.Year, GetNowQuarter(DateTime.Now), true));

            }
        }
        protected string DownlineDateTo
        {
            get
            {
                if (string.IsNullOrEmpty(DownlineDateFrom))
                {
                    return "";
                }
                else
                {
                    string todate = GetRequestValue<string>("DownlineDateTo", "");
                    if (string.IsNullOrEmpty(todate))
                        return DateTime.Now.AddDays(1).ToString("yyyy-MM-dd");
                    else
                        return todate;
                }

            }
        }

        /// <summary>
        /// 获取时间所在季度值
        /// </summary>
        /// <param name="dt"></param>
        /// <returns></returns>
        private int GetNowQuarter(DateTime dt)
        {
            int nowQuarter = 1;
            int month = dt.Month;
            if (month >= 1 && month <= 3)
                nowQuarter = 1;
            if (month >= 4 && month <= 6)
                nowQuarter = 2;
            if (month >= 7 && month <= 9)
                nowQuarter = 3;
            if (month >= 10 && month <= 12)
                nowQuarter = 4;
            return nowQuarter;
        }
        private string GetQuarterDate(int year, int quarter, bool isstart)
        {
            DateTime dt = new DateTime(year, quarter * 3, 1);
            DateTime startQuarter = dt.AddMonths(0 - (dt.Month - 1) % 3).AddDays(1 - dt.Day);  //本季度初
            DateTime endQuarter = startQuarter.AddMonths(3).AddDays(-1);  //本季度末
            return isstart ? startQuarter.ToString("yyyy-MM-dd"): endQuarter.ToString("yyyy-MM-dd");
        }
        #endregion

        protected void Page_Load(object sender, EventArgs e)
        {
            DataBind();
            AddLog(Permission.查看);
        }
        protected DataTable SUMDT = null;
        protected void DataBind()
        {
            DataTableCollection DTC = UClaimDemageControlController.GetSupplerTongJiReport(DownlineDateFrom, DownlineDateTo);
            this.rpMenu.DataSource = DTC[0];
            this.rpMenu.DataBind();
            SUMDT = DTC[1];
        }

        protected string GetUMDTValue(string filename)
        {
            if (SUMDT.Rows.Count <= 0)
            {
                return "0";
            }
            return Convert.ToDecimal(SUMDT.Rows[0][filename]).ToString("f2");
        }

    }
}